
package org.springxrsk.modules.schedule.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import jakarta.validation.Valid;

import org.springxrsk.core.secure.XrskUser;
import org.springxrsk.core.secure.annotation.PreAuth;
import org.springxrsk.core.mp.support.Condition;
import org.springxrsk.core.mp.support.Query;
import org.springxrsk.core.tool.api.R;
import org.springxrsk.core.tool.utils.Func;
import org.springxrsk.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springxrsk.modules.schedule.pojo.entity.QyPreBookEntity;
import org.springxrsk.modules.schedule.pojo.vo.QyPreBookVO;
import org.springxrsk.modules.schedule.excel.QyPreBookExcel;
import org.springxrsk.modules.schedule.wrapper.QyPreBookWrapper;
import org.springxrsk.modules.schedule.service.IQyPreBookService;
import org.springxrsk.core.boot.ctrl.XrskController;
import org.springxrsk.core.tool.utils.DateUtil;
import org.springxrsk.core.excel.util.ExcelUtil;
import org.springxrsk.core.tool.constant.XrskConstant;
import org.springxrsk.core.tool.constant.RoleConstant;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 预约 控制器
 *
 *  @author
 * @since 2024-06-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("xrsk-qyClasses/qyPreBook")
@Tag(name = "预约", description = "预约接口")
public class QyPreBookController extends XrskController {

	private final IQyPreBookService qyPreBookService;
	private final IUserService userService;

	/**
	 * 预约 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入qyPreBook")
	public R<QyPreBookVO> detail(QyPreBookEntity qyPreBook) {
		QyPreBookEntity detail = qyPreBookService.getOne(Condition.getQueryWrapper(qyPreBook));
		return R.data(QyPreBookWrapper.build().entityVO(detail));
	}
	/**
	 * 预约 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入qyPreBook")
	public R<IPage<QyPreBookVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> qyPreBook, Query query) {
		IPage<QyPreBookEntity> pages = qyPreBookService.page(Condition.getPage(query), Condition.getQueryWrapper(qyPreBook, QyPreBookEntity.class));
		IPage<QyPreBookVO> pagedVO = QyPreBookWrapper.build().pageVO(pages);
		pagedVO.getRecords().forEach(item->{
			item.setStudentName( userService.getById(item.getStudentId()).getName());
		});
		return R.data(pagedVO);
	}

	/**
	 * 预约 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入qyPreBook")
	public R<IPage<QyPreBookVO>> page(QyPreBookVO qyPreBook, Query query) {
		IPage<QyPreBookVO> pages = qyPreBookService.selectQyPreBookPage(Condition.getPage(query), qyPreBook);
		return R.data(pages);
	}

	/**
	 * 预约 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入qyPreBook")
	public R save(@Valid @RequestBody QyPreBookEntity qyPreBook) {
		return R.status(qyPreBookService.save(qyPreBook));
	}

	/**
	 * 预约 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入qyPreBook")
	public R update(@Valid @RequestBody QyPreBookEntity qyPreBook) {
		return R.status(qyPreBookService.updateById(qyPreBook));
	}

	/**
	 * 预约 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入qyPreBook")
	public R submit(@Valid @RequestBody QyPreBookEntity qyPreBook) {

		QyPreBookEntity entity = new QyPreBookEntity();
		entity.setLessonId(qyPreBook.getLessonId());
		entity.setStudentId(qyPreBook.getStudentId());
		QyPreBookEntity detail = qyPreBookService.getOne(Condition.getQueryWrapper(entity));
		if(detail == null){
			return R.status(qyPreBookService.saveOrUpdate(qyPreBook));
		}else{
			return R.status(true);
		}
	}


	/**
	 * 预约 新增或修改
	 */
	@PostMapping("/addList")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入qyPreBook")
	public R addList(@Valid @RequestBody List<QyPreBookEntity> qyPreBooklist) {
		qyPreBooklist.forEach(qyPreBook->{
			QyPreBookEntity entity = new QyPreBookEntity();
			entity.setLessonId(qyPreBook.getLessonId());
			entity.setStudentId(qyPreBook.getStudentId());
			QyPreBookEntity detail = qyPreBookService.getOne(Condition.getQueryWrapper(entity));
			if(detail == null){
				 qyPreBookService.saveOrUpdate(qyPreBook);
			}
		});
		return R.status(true);
	}


	/**
	 * 预约 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description  = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return R.status(qyPreBookService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-qyPreBook")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入qyPreBook")
	public void exportQyPreBook(@Parameter(hidden = true) @RequestParam Map<String, Object> qyPreBook, XrskUser XrskUser, HttpServletResponse response) {
		QueryWrapper<QyPreBookEntity> queryWrapper = Condition.getQueryWrapper(qyPreBook, QyPreBookEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(QyPreBook::getTenantId, XrskUser.getTenantId());
		//}
		queryWrapper.lambda().eq(QyPreBookEntity::getIsDeleted, XrskConstant.DB_NOT_DELETED);
		List<QyPreBookExcel> list = qyPreBookService.exportQyPreBook(queryWrapper);
		ExcelUtil.export(response, "预约数据" + DateUtil.time(), "预约数据表", list, QyPreBookExcel.class);
	}

}
